import requests
import re

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}

resp = requests.get('https://music.163.com/discover/toplist', headers=headers)
# print(resp.text)
html_data = resp.text

# <li><a href="/song?id=.*?">.*?</a></li>
# <li><a href="/song\?id=1951069525">精卫</a></li>
info_list = re.findall('<li><a href="/song\?id=(.*?)">(.*?)</a></li>', html_data)
for music in info_list:
    id = music[0]
    music_url = f'http://music.163.com/song/media/outer/url?id={id}'
    name = music[1]
    # 保存
    music_data = requests.get(url=music_url, headers=headers).content
    with open(f'wyy/{name}.mp3', mode='wb') as f:
        f.write(music_data)

    print(f'{name}保存成功!')